﻿using System;
using System.IO;
using System.Text;
using UnityEngine;

namespace TG
{
    public class FileWriter
    {
        private static string _path = string.Empty;
        private static string _text = string.Empty;

        public static void SetPath(int fileIndex)
        {
#if UNITY_EDITOR
            _path = Application.dataPath + $"/Config/game_{fileIndex}.txt";

#else
        _path = Application.dataPath.Replace(Application.productName + "_Data", string.Empty)+$"/game_{fileIndex}.txt";
         Debug.LogWarning(_path);
#endif
        }

        public static string GetFullPath(string fileName)
        {
            string fullPath = fileName;
#if UNITY_EDITOR
            fullPath = Application.dataPath + $"/GameConfig/{fileName}.txt";
#else
        fullPath = Application.dataPath.Replace(Application.productName + "_Data", string.Empty)+$"/{fileName}.txt";
         Debug.LogWarning(fullPath);
#endif
            return fullPath;
        }

        public static int GetFileNum()
        {
            string tmpPath;
#if UNITY_EDITOR
            tmpPath = Application.dataPath + $"/ZHDFolder/";
#else
        tmpPath = Application.dataPath.Replace(Application.productName + "_Data", string.Empty);
        Debug.LogWarning(tmpPath);
#endif
            int filesNum = 0;
            string[] files = Directory.GetFiles(tmpPath, "game_*");
            foreach (string item in files)
            {
                if (!item.Contains(".meta"))
                    filesNum++;
            }

            return filesNum;
        }

        private static string _Text
        {
            get
            {
                if (_text != string.Empty)
                {
                    return _text;
                }
                else
                {
                    if (File.Exists(_path))
                        return File.ReadAllText(_path, Encoding.GetEncoding("GBK"));
                    else
                        return string.Empty;
                }
            }
        }

        public static string Read(string str)
        {
            string[] temp = _Text.Split('\n');
            if (temp.Length == 0)
                return string.Empty;
            else
            {
                foreach (string item in temp)
                {
                    string[] setStr = item.Split('=');
                    if (setStr.Length == 0)
                        break;
                    if (str.Equals(setStr[0]))
                    {
                        return setStr[1].Replace("\r", "");
                    }
                }
            }

            return string.Empty;
        }

        public static char[] CharListParse(string str)
        {
            return Read(str).ToCharArray();
        }

        public static int IntParse(string str)
        {
            return int.Parse(Read(str));
        }

        public static float floatParse(string str)
        {
            return float.Parse(Read(str));
        }

        public static bool boolParse(string str)
        {
            return Convert.ToBoolean(IntParse(str));
        }
    }
}